/**
  ******************************************************************************
  * @file    ledapp.c
  * @author  yan 
  * @version 1.0
  * @date    2016-12-02
  * @brief   LED status control program, used to display the working state
  ******************************************************************************
	*/
  
  
/** @defgroup LED_APP
 	* @{
	 */
#include <rtthread.h>

#include "ledapp.h"
#include "led.h"
#include "string.h"
#include "mechanicaldef.h"

struct led_param{
  rt_uint32_t instence;
  rt_tick_t time;
};
/** @defgroup LED_APP_Functions
 	* @{
	 */
rt_inline void led_thread(void* parameter);

void led_thread_entry(void* parameter)
{
    
    rt_uint32_t state;
    rt_uint32_t e;
    static struct led_param lednum;
    rt_event_t ledevent = ((struct external_arg *)parameter) ->ledevent;
    
    rt_hw_led_init();
    rt_thread_t init_thread;
    while (1)
    {
      
      rt_event_recv(ledevent,0xff,RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR,RT_WAITING_FOREVER,&e);
//      if(state & e)
//        continue;
      state |= e;
      if(e == 1<<0){
        lednum.instence = 0;
        lednum.time = 50;
        init_thread = rt_thread_create("led0",led_thread,&lednum,256,20,5);
        
        if (init_thread != RT_NULL)
          rt_thread_startup(init_thread);
      }
      if(e == 1<<1){
        lednum.instence = 1;
        lednum.time = 20;
        init_thread = rt_thread_create("led1",led_thread,&lednum,256,20,5);
        
        if (init_thread != RT_NULL)
          rt_thread_startup(init_thread);
      }
      if(e == 1<<2){
        lednum.instence = 2;
        lednum.time = 10;
        init_thread = rt_thread_create("led2",led_thread,&lednum,256,20,5);
        
        if (init_thread != RT_NULL)
          rt_thread_startup(init_thread);
      }
      if(e == 1<<3){
        lednum.instence = 3;
        lednum.time = 120;
        init_thread = rt_thread_create("led3",led_thread,&lednum,256,20,5);
        
        if (init_thread != RT_NULL)
          rt_thread_startup(init_thread);
      }
    }
}

void led_thread(void* parameter)
{
  struct led_param* lednum = rt_malloc(sizeof(struct led_param));
  memcpy(lednum,parameter,sizeof(struct led_param));
//  lednum = (struct led_param *)parameter;
  unsigned int count=0;
  while(1)
  {
      /* led on */
      count++;
      rt_hw_led_on(lednum->instence);
      rt_thread_delay( lednum->time ); /* sleep 0.5 second and switch to other thread */
      
      /* led off */

      rt_hw_led_off(lednum->instence);
      rt_thread_delay( lednum->time );
  }
}


/**	
	*	@}
	*/

/**	
	*	@}
	*/
  


